package com.hope.onlinestudy.activity.integral

import com.google.gson.Gson
import com.hope.onlinestudy.base.BaseDataModel
import com.hope.onlinestudy.model.PointListModel
import com.hope.onlinestudy.model.PointModel
import com.hope.onlinestudy.mvp.BasePresenterImpl
import com.hope.onlinestudy.net.HttpNetUtils
import com.hope.onlinestudy.net.NetworkScheduler
import com.hope.onlinestudy.net.ProgressSubscriber
import com.hope.onlinestudy.utils.ApiUtils

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */
class IntegralPresenter :
    BasePresenterImpl<IntegralContract.View>(),
    IntegralContract.Presenter {
    private val allMsg: ArrayList<PointModel> by lazy { ArrayList<PointModel>() }
    override fun clearData() {
        allMsg.clear()
    }

    override fun getPoint(currentPage: Int?) {
        val msg = Gson().toJson(
            hashMapOf<String, Any>(
                "userId" to ApiUtils.loginModel?.userId!!,
                "currentPage" to currentPage!!
            )
        )
        val url =
            "${ApiUtils.configModel?.sys_base_url}${ApiUtils.configModel?.sys_h5_context}${ApiUtils.toMyPoint}"
        HttpNetUtils.getInstance().getManager()?.getUserPoint(url,ApiUtils.loginModel?.jwtToken!!, msg)
            ?.compose(NetworkScheduler.compose())
            ?.subscribe(object : ProgressSubscriber<PointListModel>(mView) {
                override fun onSuccess(data: PointListModel?, code: Int?) {
                    allMsg.addAll(data?.list!!)
                    if (allMsg.isEmpty()) {
                        mView?.showMsg("没有数据了！")
                    }
                    mView?.getPointData(data, allMsg)
                    mView?.reflashCommpand()
                }
            })
    }
}